Vitest is a Vite-native testing framework for unit and integration tests, while Playwright is used for end-to-end (E2E) testing. Both can be integrated smoothly with Svelte/SvelteKit projects.
Install Vitest and Testing Library for Svelte: npm install -D vitest @testing-library/svelte @testing-library/jest-dom
Add a test script in package.json: "test": "vitest"
Write unit tests for components using render and fireEvent from @testing-library/svelte.
Install Playwright: npm install -D @playwright/test
Initialize Playwright: npx playwright install
Write E2E tests that simulate real user interactions in the browser.
Run tests with npx playwright test.
Use Vitest for fast, isolated unit and integration tests.
Use Playwright for full E2E tests including routing and server interactions.
Mock external APIs during unit tests to ensure determinism.
Automate tests in CI/CD pipelines for continuous verification.
Keep E2E tests stable and minimize reliance on fragile selectors.